package com.company.oa.hr.service.employee.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.oa.common.exception.BusinessException;
import com.company.oa.hr.dto.employee.EmployeeDTO;
import com.company.oa.hr.entity.employee.Employee;
import com.company.oa.hr.mapper.employee.EmployeeMapper;
import com.company.oa.hr.service.employee.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Override
    public IPage<Map<String, Object>> getEmployeePage(int page, int size, Employee employee) {
        Page<Employee> pageParam = new Page<>(page, size);
        return baseMapper.selectEmployeePageWithDeptAndPosition(pageParam, employee);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createEmployee(EmployeeDTO employeeDTO, Long creatorId) {
        // 检查员工编号是否已存在
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getEmployeeNo, employeeDTO.getEmployeeNo());
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException("员工编号已存在");
        }
        
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        
        // 计算试用期结束日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(employeeDTO.getEntryDate());
        calendar.add(Calendar.MONTH, employeeDTO.getProbationMonths());
        employee.setProbationEndDate(calendar.getTime());
        
        // 设置工作状态为试用期
        employee.setWorkStatus(1);
        
        // 设置创建信息
        employee.setCreatorId(creatorId);
        employee.setCreateTime(new Date());
        
        return save(employee);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateEmployee(EmployeeDTO employeeDTO) {
        // 检查员工是否存在
        Employee existEmployee = getById(employeeDTO.getId());
        if (existEmployee == null) {
            throw new BusinessException("员工不存在");
        }
        
        // 检查员工编号是否已被其他员工使用
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getEmployeeNo, employeeDTO.getEmployeeNo())
                   .ne(Employee::getId, employeeDTO.getId());
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException("员工编号已被其他员工使用");
        }
        
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        
        // 计算试用期结束日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(employeeDTO.getEntryDate());
        calendar.add(Calendar.MONTH, employeeDTO.getProbationMonths());
        employee.setProbationEndDate(calendar.getTime());
        
        // 设置更新时间
        employee.setUpdateTime(new Date());
        
        return updateById(employee);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteEmployee(Long id) {
        // 检查员工是否存在
        Employee existEmployee = getById(id);
        if (existEmployee == null) {
            throw new BusinessException("员工不存在");
        }
        
        return removeById(id);
    }

    @Override
    public Map<String, Object> importEmployees(MultipartFile file, Long creatorId) {
        // 实际项目中需要实现Excel导入逻辑
        Map<String, Object> result = new HashMap<>();
        result.put("success", 0);
        result.put("fail", 0);
        result.put("total", 0);
        result.put("failList", null);
        return result;
    }

    @Override
    public byte[] exportEmployees(List<Long> employeeIds) {
        // 实际项目中需要实现Excel导出逻辑
        return new byte[0];
    }

    @Override
    public Map<String, Object> getEmployeeDetail(Long id) {
        return baseMapper.selectEmployeeDetailById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean employeeResign(Long id, String reason, Date resignDate) {
        // 检查员工是否存在
        Employee existEmployee = getById(id);
        if (existEmployee == null) {
            throw new BusinessException("员工不存在");
        }
        
        // 更新员工状态为离职
        Employee employee = new Employee();
        employee.setId(id);
        employee.setWorkStatus(3); // 离职状态
        employee.setRemark(reason);
        employee.setUpdateTime(new Date());
        
        return updateById(employee);
    }
} 